package hesso.smartdraw.ch;

import hesso.smartdraw.ch.db.SyncDbAndDb;
import hesso.smartdraw.ch.soap.SOAPHelper;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.Toast;


public class LoginRegisterScreen extends TabActivity {

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.loginscreen);

        
        //Resources res = getResources();
        TabHost tabHost = getTabHost();
        TabHost.TabSpec spec;


        spec = tabHost.newTabSpec("login").setIndicator("Login").setContent(R.id.loginLayout);	
        tabHost.addTab(spec);
        


        spec = tabHost.newTabSpec("register").setIndicator("Register").setContent(R.id.registerLayout);
        tabHost.addTab(spec);
        


        tabHost.setCurrentTab(0);
     
        
        final Button buttonRegister = (Button) findViewById(R.id.btnRegister);
        buttonRegister.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	
            	EditText emailText = (EditText)findViewById(R.id.txtEmailRegister);
            	
            	boolean isRegistered = SOAPHelper.registerClient(emailText.getText().toString());
            	
            	
            	if(!isRegistered) {
            		

            	} else {
            		
            		Intent intent = new Intent(LoginRegisterScreen.this,LoginRegisterScreen.class);

                	startActivity(intent);
            	}
            	
            }
        });
        
        final Button button = (Button) findViewById(R.id.btnLogin);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
			public void onClick(View v) {
            	
            	EditText emailText = (EditText)findViewById(R.id.txtEmail);
            	EditText pwText = (EditText)findViewById(R.id.txtPassword);
            	
            	int userId = SOAPHelper.loginCheckId(emailText.getText().toString(),pwText.getText().toString());
            	
            	
            	if(userId == -1) {
            		
            		wrong();
            	} else {
            		
            		SyncDbAndDb sync = SyncDbAndDb.getInstance();

                    sync.startProgram(userId);
            		
            		Intent intent = new Intent(LoginRegisterScreen.this,BuildingScreen.class);
            		
            		
                	intent.putExtra("userId", userId);

                	startActivity(intent);
            	}
            }
        });
        
    }
    
    public void wrong() {
    	
    	Toast.makeText(this, "Wrong password or email", Toast.LENGTH_LONG).show();
    }
    

}
